﻿'========================================================================
'功能:VIP設定作業 
'作者:shihyu    
'日期: 2011/05/23
'========================================================================
Imports System.Data
Partial Class WebForm_Mail_VIPMaintain
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            If Not ChkProgramRight(1) Then '查詢
                Me.btnQuery.Visible = False
                Me.gvVIP.Visible = False
            End If

            If Not ChkProgramRight(2) Then '新增
                Me.btnInsert.Visible = False
                Panel1.Visible = False
            End If

            If Not ChkProgramRight(3) Then '編輯
                Me.gvVIP.Columns(0).Visible = False
            End If

            If Not ChkProgramRight(4) Then '刪除
                Me.gvVIP.Columns(1).Visible = False
            End If
        End If

    End Sub

    Protected Sub BindGV(ByVal EmpID As String, ByVal Agent As String)
        Me.gvVIP.Visible = True
        Dim gvDB As AssetManage.BBL.VIPBBL = New AssetManage.BBL.VIPBBL()
        Dim gBBl As OAMS.BBL.GroupBBL = New OAMS.BBL.GroupBBL()

        Me.gvVIP.DataSource = gvDB.GetVIP(dbName, EmpID, Agent, gBBl.GroupMemberIsExist("Admin", hiUID.Value, hiDBName.Value), hiUID.Value)
        Me.gvVIP.DataBind()
    End Sub




    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV(Me.txtEmpID.Text.Trim(), Me.txtAgent.Text.Trim())
    End Sub

    Protected Sub gvVIP_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvVIP.RowCancelingEdit
        Me.gvVIP.EditIndex = -1
        Me.BindGV(Me.txtEmpID.Text.Trim(), Me.txtAgent.Text.Trim())
    End Sub


    Protected Sub gvVIP_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvVIP.RowUpdated
        Me.gvVIP.EditIndex = -1
        Me.BindGV(Me.txtEmpID.Text.Trim(), Me.txtAgent.Text.Trim())
    End Sub
    Protected Sub gvVIP_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvVIP.RowDeleting
        Dim empID As String = CType(gvVIP.Rows(e.RowIndex).FindControl("lblgvEmpID"), Label).Text.Trim
        Dim AgentEmpID As String = CType(gvVIP.Rows(e.RowIndex).FindControl("lblgvAgentEmpID"), Label).Text.Trim

        Dim del As AssetManage.BBL.VIPBBL = New AssetManage.BBL.VIPBBL()
        Me.gvVIP.EditIndex = -1
        del.DeleteVIP(dbName, empID, AgentEmpID)
        Me.BindGV(Me.txtEmpID.Text.Trim(), Me.txtAgent.Text.Trim())
    End Sub

    Protected Sub gvVIP_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvVIP.RowEditing
        Me.gvVIP.EditIndex = e.NewEditIndex
        Me.BindGV(Me.txtEmpID.Text.Trim(), Me.txtAgent.Text.Trim())
    End Sub

    Protected Sub gvVIP_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvVIP.RowUpdating
        Dim empID As String = CType(gvVIP.Rows(e.RowIndex).FindControl("lblgvEmpID"), Label).Text.Trim
        Dim AgentEmpID As String = CType(gvVIP.Rows(e.RowIndex).FindControl("txtgvAgentEmpID"), TextBox).Text.Trim

        Dim gvUpdate As AssetManage.BBL.VIPBBL = New AssetManage.BBL.VIPBBL()
        Dim uBLL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        'If uBLL.UserIsExist(AgentEmpID, dbName) Then

        '    BasePage.ShowMessageForAjax(btnQuery, GetLocalResourceObject("EmpAgentEmpty"))
        'ElseIf gvUpdate.VIPIsExist(dbName, empID, AgentEmpID) Then
        '    BasePage.ShowMessageForAjax(btnQuery, GetLocalResourceObject("AgentExist"))
        'Else
        If AgentEmpID <> "" Then
            If uBLL.UserIsExist(AgentEmpID, dbName) = False Then

                BasePage.ShowMessageForAjax(btnQuery, GetLocalResourceObject("EmpAgentEmpty"))
                Exit Sub
            End If
           
        End If
        gvUpdate.UpdateVIPDept(dbName, empID, AgentEmpID, hiUID.Value)
        e.Cancel = True
        Me.gvVIP.EditIndex = -1
        txtEmpID.Text = ""
        txtAgent.Text = ""
        Me.BindGV(txtEmpID.Text.Trim(), txtAgent.Text.Trim())
    End Sub

    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        txtVipEmpID.Text = ""
        txtVipAgent.Text = ""
        '' Me.mpSearch.Show()
    End Sub

    Protected Sub btnVipSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVipSave.Click

        Dim uBLL As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        Dim dept1, dept2 As String
        Dim gBBL As OAMS.BBL.GroupBBL = New OAMS.BBL.GroupBBL()

        If uBLL.UserIsExist(Me.txtVipEmpID.Text.Trim(), dbName) = False Then
            BasePage.ShowMessageForAjax(btnVipSave, GetLocalResourceObject("EmpEmpty"))
            Me.mpSearch.Show()
            Exit Sub
        End If
        If txtVipAgent.Text <> "" Then
            If uBLL.UserIsExist(Me.txtVipAgent.Text.Trim(), dbName) = False Then
                BasePage.ShowMessageForAjax(btnVipSave, GetLocalResourceObject("EmpAgentEmpty"))
                Me.mpSearch.Show()
                Exit Sub
            End If
        End If


        Dim VipSave As AssetManage.BBL.VIPBBL = New AssetManage.BBL.VIPBBL()

        If VipSave.VIPIsExist(dbName, Me.txtVipEmpID.Text.Trim(), Me.txtVipAgent.Text.Trim()) Then
            BasePage.ShowMessageForAjax(btnVipSave, GetLocalResourceObject("AgentExist"))
            Me.mpSearch.Show()
        Else
            If gBBL.GroupMemberIsExist("Admin", hiUID.Value, hiDBName.Value) = False Then
                If txtVipAgent.Text <> "" Then
                    dept1 = uBLL.GetUsers(hiUID.Value, hiDBName.Value).Rows(0)("Dept_ID").ToString
                    If dept1.Length > 2 Then
                        dept1 = dept1.Substring(0, 2)
                    End If

                    dept2 = uBLL.GetUsers(txtVipEmpID.Text, hiDBName.Value).Rows(0)("Dept_ID").ToString
                    If dept2.Length > 2 Then
                        dept2 = dept2.Substring(0, 2)
                    End If

                    If dept1 <> dept2 Then
                        ShowMessageForAjax(btnVipSave, GetLocalResourceObject("ErrDept"))
                        mpSearch.Show()
                        Exit Sub
                    End If
                   

                    dept2 = uBLL.GetUsers(txtVipAgent.Text, hiDBName.Value).Rows(0)("Dept_ID").ToString
                    If dept2.Length > 2 Then
                        dept2 = dept2.Substring(0, 2)
                    End If

                    If dept1 <> dept2 Then
                        ShowMessageForAjax(btnVipSave, GetLocalResourceObject("ErrDept"))
                        mpSearch.Show()
                        Exit Sub
                    End If
                End If
            End If

            VipSave.InsertVIP(dbName, Me.txtVipEmpID.Text.Trim(), Me.txtVipAgent.Text.Trim(), hiUID.Value)
            Me.gvVIP.EditIndex = -1
            Me.BindGV(Me.txtEmpID.Text.Trim(), Me.txtAgent.Text.Trim())
        End If


    End Sub


    Protected Sub gvVIP_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles gvVIP.RowDeleted
        Me.gvVIP.EditIndex = -1
        Me.BindGV(Me.txtEmpID.Text.Trim(), Me.txtAgent.Text.Trim())
    End Sub

   
    Protected Sub gvVIP_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvVIP.PageIndexChanging
        Me.gvVIP.EditIndex = -1
        gvVIP.PageIndex = e.NewPageIndex
        Me.BindGV(Me.txtEmpID.Text.Trim(), Me.txtAgent.Text.Trim())
    End Sub

    Protected Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        PopupFormForAjax(btnOpen, "emp_no,emp_name", GetLocalResourceObject("colUsers"), "AUOREF.dbo.emp_data_all", hiDBName.Value, "1", txtAgent.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")

    End Sub

    Protected Sub btnOpenIns_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenIns.Click
        PopupFormForAjax(btnOpen, "emp_no,emp_name", GetLocalResourceObject("colUsers"), "AUOREF.dbo.emp_data_all", hiDBName.Value, "1", txtVipAgent.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")
        mpSearch.Show()
    End Sub

    Protected Sub btnOpenIns0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenIns0.Click
        PopupFormForAjax(btnOpen, "emp_no,emp_name", GetLocalResourceObject("colUsers"), "AUOREF.dbo.emp_data_all", hiDBName.Value, "1", txtVipEmpID.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")
        mpSearch.Show()
    End Sub

    Protected Sub gvVIP_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvVIP.RowCommand
        If e.CommandName = "Open" Then
            PopupFormForAjax(btnOpen, "emp_no,emp_name", GetLocalResourceObject("colUsers"), "AUOREF.dbo.emp_data_all", hiDBName.Value, "1", gvVIP.Rows(gvVIP.EditIndex).FindControl("txtgvAgentEmpID").ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")

        End If
    End Sub
End Class
